package com.hiultra.archive.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.hiultra.archive.domain.*;
import com.hiultra.archive.mapper.*;
import com.hiultra.archive.service.IArchiveBoxStatusService;
import com.hiultra.common.constant.BusinessConstants;
import com.hiultra.common.core.domain.AjaxResult;
import com.hiultra.common.utils.DateUtils;
import com.hiultra.common.utils.StringUtils;
import com.hiultra.common.utils.uuid.UUID;
import com.hiultra.common.utils.uuid.UUIDUtil;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.hiultra.archive.service.IArchiveStocktakingBoxService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 盒盘点上送结果数据Service业务层处理
 *
 * @author paidaxing
 * @date 2024-07-31
 */
@Service
public class ArchiveStocktakingBoxServiceImpl implements IArchiveStocktakingBoxService {
    // 单位编号
    @Value("${orgNo}")
    private String orgNo;
    // 档案室编号
    @Value("${roomNo}")
    private String roomNo;
    @Autowired
    private ArchiveStocktakingBoxMapper archiveStocktakingBoxMapper;
    @Autowired
    private ArcBoxMapper arcBoxMapper;
    @Autowired
    private ArchiveBoxStatusMapper archiveBoxStatusMapper;
    @Autowired
    private ArchiveStocktakingMapper archiveStocktakingMapper;
    @Autowired
    private ArchiveStocktakingTaskMapper archiveStocktakingTaskMapper;
    @Autowired
    private IArchiveBoxStatusService archiveBoxStatusService;

    /**
     * 查询盒盘点上送结果数据
     *
     * @param id 盒盘点上送结果数据主键
     * @return 盒盘点上送结果数据
     */
    @Override
    public ArchiveStocktakingBox selectArchiveStocktakingBoxById(String id) {
        return archiveStocktakingBoxMapper.selectArchiveStocktakingBoxById(id);
    }

    /**
     * 查询盒盘点上送结果数据列表
     *
     * @param archiveStocktakingBox 盒盘点上送结果数据
     * @return 盒盘点上送结果数据
     */
    @Override
    public List<ArchiveStocktakingBox> selectArchiveStocktakingBoxList(ArchiveStocktakingBox archiveStocktakingBox) {
        return archiveStocktakingBoxMapper.selectArchiveStocktakingBoxList(archiveStocktakingBox);
    }

    /**
     * 查询盒盘点上送结果数据列表盘盈和盘亏
     *
     * @param archiveStocktakingBox 盒盘点上送结果数据
     * @return 盒盘点上送结果数据集合
     */
    public List<ArchiveStocktakingBox> selectArchiveStocktakingBoxListReport(ArchiveStocktakingBox archiveStocktakingBox){
        return archiveStocktakingBoxMapper.selectArchiveStocktakingBoxListReport(archiveStocktakingBox);
    }

    /**
     * 新增盒盘点上送结果数据
     *
     * @param archiveStocktakingBox 盒盘点上送结果数据
     * @return 结果
     */
    @Override
    public int insertArchiveStocktakingBox(ArchiveStocktakingBox archiveStocktakingBox) {
        return archiveStocktakingBoxMapper.insertArchiveStocktakingBox(archiveStocktakingBox);
    }

    /**
     * 修改盒盘点上送结果数据
     *
     * @param archiveStocktakingBox 盒盘点上送结果数据
     * @return 结果
     */
    @Override
    public int updateArchiveStocktakingBox(ArchiveStocktakingBox archiveStocktakingBox) {
        return archiveStocktakingBoxMapper.updateArchiveStocktakingBox(archiveStocktakingBox);
    }

    /**
     * 批量删除盒盘点上送结果数据
     *
     * @param ids 需要删除的盒盘点上送结果数据主键
     * @return 结果
     */
    @Override
    public int deleteArchiveStocktakingBoxByIds(String[] ids) {
        return archiveStocktakingBoxMapper.deleteArchiveStocktakingBoxByIds(ids);
    }

    /**
     * 删除盒盘点上送结果数据信息
     *
     * @param id 盒盘点上送结果数据主键
     * @return 结果
     */
    @Override
    public int deleteArchiveStocktakingBoxById(String id) {
        return archiveStocktakingBoxMapper.deleteArchiveStocktakingBoxById(id);
    }

    /**
     * 批量新增盒盘点上送结果数据
     *
     * @param archiveStocktakingBoxList 档案盒盘点数据列表
     * @return 结果
     */
    @Override
    public int batchinsertArchiveStocktakingBox(List<ArchiveStocktakingBox> archiveStocktakingBoxList) {
        if (ObjectUtil.isEmpty(archiveStocktakingBoxList)) {
            return 0;
        }
        return archiveStocktakingBoxMapper.batchinsertArchiveStocktakingBox(archiveStocktakingBoxList);
    }

    /**
     * 根据任务id查询盒详情
     *
     * @param taskId
     * @return
     */
    @Override
    public List<ArchiveStocktakingBox> selectBoxByTaskId(String taskId) {
        return archiveStocktakingBoxMapper.selectBoxByTaskId(taskId);
    }

    /**
     * 后台用接口——新增盒盘点上送结果数据
     *
     * @param archiveStocktakingBox
     * @return
     */
    @Override
    public AjaxResult insertStocktakingBox(ArchiveStocktakingBox archiveStocktakingBox) {

        ArcBox arcBoxByBoxCode = arcBoxMapper.selectArcBoxByBoxCode(archiveStocktakingBox.getBoxNo());
        if (StringUtils.isNull(arcBoxByBoxCode)) {
//            ArcBox arcBox = new ArcBox();
            ArchiveBoxStatus archiveBoxStatus = new ArchiveBoxStatus();
//            arcBox.setRoomNo(roomNo);
//            arcBox.setOrgNo(orgNo);
//            arcBox.setHouseLocationNo(archiveStocktakingBox.getHouseLocationNo());
            archiveBoxStatus.setBoxBarCode(archiveStocktakingBox.getBoxBarCode());
            if (StringUtils.isNotNull(archiveStocktakingBox.getActualHouseLocationNo())) {
//                arcBox.setHouseLocationNo(archiveStocktakingBox.getActualHouseLocationNo());
                archiveBoxStatus.setHouseLocationNo(archiveStocktakingBox.getActualHouseLocationNo());
            }
            if (StringUtils.isNotNull(archiveStocktakingBox.getBoxBarCode())) {
//                arcBox.setBoxRfid(archiveStocktakingBox.getBoxBarCode());
                archiveBoxStatus.setBoxBarCode(archiveStocktakingBox.getBoxBarCode());
//                arcBox.setStatus(1);
            }
//            arcBox.setCabinetNo(archiveStocktakingBox.getColNo());
//            arcBox.setAreaNo(archiveStocktakingBox.getArchAreaNo());
//            arcBox.setGiveBackStatus("2");//未借阅
//            arcBox.setBoxCode(archiveStocktakingBox.getBoxNo());
//            arcBox.setInStockStatus("1");

            archiveBoxStatus.setCabinetNo(archiveStocktakingBox.getColNo());
            archiveBoxStatus.setBoxNo(archiveStocktakingBox.getBoxNo());
            archiveBoxStatus.setBoxStatus("1");//8 入库
            archiveBoxStatus.setRecordTime(new Date());
            archiveBoxStatus.setPushStatus(BusinessConstants.PUSHSTATUS);

            archiveBoxStatusMapper.insertArchiveBoxStatus(archiveBoxStatus);
            //todo 档案室档案盒状态上报
            //archiveBoxStatusService.reportArchiveBoxStatus(archiveBoxStatus);
//            arcBox.setBoxId(UUID.randomUUID().toString().replace("-", ""));
//            arcBoxMapper.insertArcBox(arcBox);
        } else {
            return AjaxResult.error("当前盒已存在,请修改后重试！");
        }
        archiveStocktakingBox.setId(UUIDUtil.getUUID32());

        // 盘盈档案盒生成一个盒号
        String boxNo;
        while (true) {
            boxNo = roomNo + RandomStringUtils.randomNumeric(6);
            List<ArchiveStocktakingBox> stocktakingBoxes = archiveStocktakingBoxMapper.selectBoxByBoxNo(boxNo);
            if(stocktakingBoxes == null || stocktakingBoxes.isEmpty()){
                break;
            }
        }
        archiveStocktakingBox.setBoxNo(boxNo);
        archiveStocktakingBox.setBoxBarCode(boxNo);
        int result = archiveStocktakingBoxMapper.insertArchiveStocktakingBox(archiveStocktakingBox);
        if(result == 1){
            return AjaxResult.success(archiveStocktakingBox);
        }else{
            return AjaxResult.error("新增盘盈档案盒失败");
        }
    }

    /**
     * 修改盒盘点上送结果数据
     *
     * @param archiveStocktakingBox
     * @return
     */
    @Override
    @Transactional
    public int editStocktakingBox(ArchiveStocktakingBox archiveStocktakingBox) {
        archiveStocktakingBox.setInventoryStatus(BusinessConstants.END_INVENTORY_ORDER_STATUS);
        ArcBox arcBox = arcBoxMapper.selectArcBoxByBoxCode(archiveStocktakingBox.getBoxNo());
        //archiveStocktakingBox.setInventoryResult();

        if (!StringUtils.isNull(arcBox)) {
            if (StringUtils.isNotNull(archiveStocktakingBox.getHouseLocationNo())) {
                arcBox.setHouseLocationNo(archiveStocktakingBox.getHouseLocationNo());
            }
            if (StringUtils.isNotNull(archiveStocktakingBox.getActualHouseLocationNo())) {
                arcBox.setHouseLocationNo(archiveStocktakingBox.getActualHouseLocationNo());
            }
            if (StringUtils.isNotNull(archiveStocktakingBox.getBoxBarCode())) {
                arcBox.setBoxRfid(archiveStocktakingBox.getBoxBarCode());
                arcBox.setStatus(BusinessConstants.IS_WRITE_CARD);
            } else {
                arcBox.setStatus(BusinessConstants.PRE_IS_WRITE_CARD);
            }
            if (StringUtils.isNotNull(archiveStocktakingBox.getColNo())) {
                arcBox.setCabinetNo(archiveStocktakingBox.getColNo());
            }
            if (StringUtils.isNotNull(archiveStocktakingBox.getArchAreaNo())) {
                arcBox.setAreaNo(archiveStocktakingBox.getArchAreaNo());
            }
            arcBox.setBoxCode(archiveStocktakingBox.getBoxNo());

            //新增盘点档案盒状态
            ArchiveBoxStatus archiveBoxStatus = new ArchiveBoxStatus();
            archiveBoxStatus.setBoxBarCode(archiveStocktakingBox.getBoxBarCode());
            archiveBoxStatus.setHouseLocationNo(archiveStocktakingBox.getHouseLocationNo());
            archiveBoxStatus.setAreaNo(archiveStocktakingBox.getArchAreaNo());
            archiveBoxStatus.setCabinetNo(archiveStocktakingBox.getColNo());
            archiveBoxStatus.setBoxNo(archiveStocktakingBox.getBoxNo());
            archiveBoxStatus.setBoxStatus(BusinessConstants.ARCHIVE_STOCKING);
            archiveBoxStatus.setRecordTime(DateUtils.getNowDate());
            archiveBoxStatus.setPushStatus(BusinessConstants.PUSHSTATUS);

            arcBoxMapper.updateArcBox(arcBox);
            archiveBoxStatusMapper.insertArchiveBoxStatus(archiveBoxStatus);
            archiveStocktakingBoxMapper.updateArchiveStocktakingBox(archiveStocktakingBox);
            //todo 档案室档案盒状态上报
            //archiveBoxStatusService.reportArchiveBoxStatus(archiveBoxStatus);

            //根据taskid 查询是否都有盘点结果，如果都有则判定为盘点结束
//            ArchiveStocktakingBox archiveStocktakingBox1 = new ArchiveStocktakingBox();
//            archiveStocktakingBox1.setTaskId(archiveStocktakingBox.getTaskId());
//            ArchiveStocktaking archiveStocktaking = new ArchiveStocktaking();
//            archiveStocktaking.setTaskId(archiveStocktakingBox.getTaskId());
//            List<ArchiveStocktakingBox> archiveStocktakingBoxList = archiveStocktakingBoxMapper.selectArchiveStocktakingBoxList(archiveStocktakingBox1);
//            List<ArchiveStocktaking> archiveStocktakingList = archiveStocktakingMapper.selectArchiveStocktakingList(archiveStocktaking);

            //判断是否盘点完成
//            int inventoryResultNum = 0;
//            for (ArchiveStocktakingBox archiveStocktakingBox2 : archiveStocktakingBoxList) {
//                if (ObjectUtil.isEmpty(archiveStocktakingBox2.getInventoryResult())) {
//                    inventoryResultNum++;
//                    break;
//                }
//            }
//            for (ArchiveStocktaking archiveStocking : archiveStocktakingList) {
//                if (ObjectUtil.isEmpty(archiveStocking.getInventoryResult())) {
//                    inventoryResultNum++;
//                    break;
//                }
//            }
//            if (inventoryResultNum <= 0) {
//                archiveStocktakingTaskMapper.updateArchiveStocktakingTask(new ArchiveStocktakingTask() {{
//                    setTaskId(archiveStocktakingBox.getTaskId());
//                    setInventoryTime(DateUtils.getNowDate());
//                    setStatus(BusinessConstants.END_INVENTORY_ORDER_STATUS);
//                }});
//            }
        }
        return 1;
    }
}
